set more off
clear all

cap log close
log using an_did_pat_placebo,t replace

*** Program to compute TFP response to patent use, using industry-level TFP estimates

use sample, clear
sort id year
drop tfp 
rename firstyr_pat firstyr

merge 1:1 id year using tfp_ind

count if patold & id~=id[_n-1]
drop if patold /* Analysis excludes long term trademark users */

drop if _m==1
drop _m

tab pat
gen patany = patnew | patold
tab patany
tab patold
tab patnew if ~patold

******************************************************************************
*** construct placebo sample
******************************************************************************
sort id year
set seed 4639512
drop firstyr minyear
gen evertreat = uniform()<.144 if id~=id[_n-1]
egen x = mean(evertreat), by(id)
replace evertreat = x 
drop x
egen nt = count(year), by(id)
egen minyear = min(year), by(id)
gen firstyr = int(nt*uniform()) + minyear if id~=id[_n-1] & evertreat
egen x = mean(firstyr), by(id)
replace firstyr = x
replace firstyr = . if firstyr<1996 /* no prior periods in data */

keep log* tfp* pat* year id firstyr ind logel
aorder
sum
tab year,gen(y_)
global yrdum y_3-y_11
xtset id year
gen trend = year-1995

gen control = firstyr==0
gen ttrend = (~control)*trend
  
gen lag = year-firstyr
replace lag = 0 if lag==.
tab lag,gen(L_)
global lagdum L_2 L_3 L_4 L_5 L_6 L_7 L_8 L_9 L_10 L_12 L_13 L_14 L_15 L_16 L_17 L_18 L_19 L_20
replace L_11 = 0 if control

egen x = max(year),by(id)
replace firstyr = x+1 if control
drop x
gen before = firstyr>=year 
gen after = firstyr<year 
gen atrend = lag*after
gen btrend = before*lag
tab before after
*drop if firstyr==year 
drop if loga==.

*** Count the firms with first-time use
egen x = sum(patnew),by(id)
gen first_time = x>0
tab first_time if id~=id[_n-1]
drop x 

*** Regressions for firm variables including tfp with a dummy for post-patent use

matrix btemp1 = J(9,1,0)
matrix btemp2 = J(9,1,0)
global slist logs loge loga logm tfp_acf tfp_acf_i
tabstat $slist, by(year)

foreach var of varlist $slist { 
    qui xtreg `var' after $yrdum, robust fe
    est store `var'4
    qui xtreg `var' btrend atrend $yrdum, robust fe
    est store `var'5
    qui xtreg `var' $lagdum $yrdum, robust fe
    est store `var'6
	matrix beta = (get(_b))'
	matrix btemp1 = beta["L_2".."L_10","y1"]
	matrix btemp2 = beta["L_12".."L_20","y1"]
	matrix b`var' = J(19,1,0)
	matrix b`var' = btemp1\0\btemp2
  }

esttab *4, keep(after) ///
       b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust) nogaps
esttab *5, keep(btrend atrend) ///
       b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust) nogaps
esttab *6, keep($lagdum) ///
       b(%10.3f) se(%10.3f) star stats(r2 rmse F df_m N N_clust) nogaps 
	   
*** Lag coefficients for graphs	   
esttab *6, keep($lagdum) ///
       b(%10.4f) not nostar stats(r2 rmse F df_m N N_clust) nogaps wide   

matrix LAG = J(19,6,0)
matrix LAG = blogs,bloge,bloga,blogm,btfp_acf,btfp_acf_i
matrix colnames LAG = llogs lloge lloga llogm ltfp_acf ltfp_acf_i
matrix rownames LAG = l2 l3 l4 l5 l6 l7 l8 l9 l10 l11 l12 l13 l14 l15 l16 l17 l18 l19 l20
svmat LAG, names(col)
replace lag = _n-10   /*  timing variable  */
keep if lag>-6 & lag<6 & lag~=.
scatter llog* ltfp* lag, connect(l l l l l l) msymbol(i i i i i i) ///
       title("Placebo test for first patent use")

log close
